package com.southgis.ibase.sql.builder;

import java.util.Objects;

import org.apache.commons.lang3.StringUtils;

/**
 * 原生的表描述
 * @author dnnyo
 *
 */
class SimpleTable extends Table
{
	private String name;
	SimpleTable(String name, String alias)
	{
		super(alias);
		this.name=name;
	}
	
	@Override
	public String getAlias()
	{
		String temp=alias;
		if(StringUtils.isEmpty(temp))
			temp=name;
		return temp;
	}
	
	@Override
	public boolean matchName(String name)
	{
		boolean br=super.matchName(name);
		return br || this.name.equalsIgnoreCase(name);
	}

	@Override
	public void toSql(int section,StringBuilder sb)
	{
		//0-select，1-where|having，2-XBy，3-join|from
		if(section==SqlBuilder.BUILD_SECTION_FROMJOIN) {
			sb.append(name);
			if(!StringUtils.isEmpty(alias))
				sb.append(" ").append(alias);
		}else if(section==SqlBuilder.BUILD_SECTION_DEINSERT) {
			sb.append(name);
		}else {
			sb.append(getAlias());
		}
	}
	
	@Override
	public boolean equals(Object obj)
	{
		if(obj==null) return false;
		if(this==obj) return true;
		if(!(obj instanceof SimpleTable)) return false;
		return Objects.equals(this.name, ((SimpleTable)obj).name);
	}
	@Override
	public int hashCode()
	{
		return Objects.hash(this.getClass(), this.name);
	}

	@Override
	public String toString()
	{
		return (name==null?"":name)+(alias!=null?("["+alias+"]"):"");
	}
}